﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using ExtendModule;


namespace LabMS.Web.Equipment.Valuables
{
    public partial class valuablespreview : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("GZSBGL");//必须写在ispostback外
            strPageUserType.Add("mana");
            strPageUserType.Add("student");
            strPageUserType.Add("teacher");

            tbCreateDateMin.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this)");
            tbCreateDateMax.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this)");
            //checkall.Attributes.Add("onclick", "checkAllChanged(this, 'GridViewValuables')");
            if (!Page.IsPostBack)
            {
                bind("");
                lbDelete.Attributes.Add("onclick", "javascript:return confirm('你确认要删除这些易耗品吗?')");
            }
            tbCreateDateMin.Attributes.Add("readOnly", "-1");
            tbCreateDateMax.Attributes.Add("readOnly", "-1");
        }

        public void bind(string strWhere)
        {
            List<ExtendModule.InstrumentAndConsumables> listevaluables = new
               List<InstrumentAndConsumables>();
            ExtendBLL.InstrumentAndConsumables bevaluables = new ExtendBLL.InstrumentAndConsumables();
            listevaluables = bevaluables.GetExtendModelList(" Type = 'guizhongyiqi' " + strWhere + " order by ID desc ");

            LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
            listCClass = eDictionary.GetItemListByDClass("haocaileibie");
            foreach (ExtendModule.InstrumentAndConsumables i in listevaluables)
            {
                mLab = blab.GetModelList(" Lab_Code='" + i.StockLocation + "'");
                if (mLab.Count > 0)
                {
                    i.StockLocation = mLab[0].Lab_Name;
                }
                listCClass = eDictionary.GetModelList(" DClass='shebeilaiyuan' and Code='" + i.Source + "' ");
                if (listCClass.Count > 0)
                {
                    i.Source = listCClass[0].Name;
                }
                listCClass = eDictionary.GetModelList(" DClass='shiyongfangxiang' and Code='" + i.Arrow + "' ");
                if (listCClass.Count > 0)
                {
                    i.Arrow = listCClass[0].Name;
                }
                listCClass = eDictionary.GetModelList(" DClass='shebeizhuangtai' and Code='" + i.PresentSituation + "' ");
                if (listCClass.Count > 0)
                {
                    i.PresentSituation = listCClass[0].Name;
                }
            }

            GridViewValuables.DataSource = listevaluables;
            GridViewValuables.DataBind();
        }

        protected void GridViewValuables_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewValuables.PageIndex = e.NewPageIndex;
            if (tbKeyWords.Text.Trim().Equals("") && tbCreateDateMin.Text.Trim().Equals("") &&
                     tbCreateDateMax.Text.Trim().Equals("") && tbPurpose.Text.Trim().Equals(""))
            {
                bind("");
            }
            else
            {
                bind(SearchString());
            }
        }

        protected void GridViewValuables_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["style"] = "Cursor:pointer";
                int itemp = e.Row.Cells.Count;
                for (int i = 1; i < itemp; i++)
                {
                    e.Row.Cells[i].Attributes.Add("OnDblClick", "window.location.href='valuablesdetail.aspx?ID=" + GridViewValuables.DataKeys[GridViewValuables.DataKeys.Count - 1].Value.ToString() + "'");
                }
            }
        }

        //搜索查询语句
        protected string SearchString()
        {
            string strWhere = " and " + ddlCClass.SelectedValue + " like '%" + tbKeyWords.Text.Trim() + "%' ";
            if (!string.IsNullOrEmpty(tbCreateDateMin.Text.Trim()))
            {
                strWhere += " and CreatedDate >= '" + tbCreateDateMin.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(tbCreateDateMax.Text.Trim()))
            {
                strWhere += " and CreatedDate <= '" + tbCreateDateMax.Text.Trim() + "'";
            }
            strWhere += " and Purpose like '%" + tbPurpose.Text.Trim() + "%'";

            return strWhere;
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            string strWhere = " and Name like '%" + tbKeyWords.Text.Trim() + "%' ";
            bind(SearchString());
        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {
            tbKeyWords.Text = "";
            tbPurpose.Text = "";
            tbCreateDateMin.Text = "";
            tbCreateDateMax.Text = "";
            bind("");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            CheckBox ch;
            LabMS.BLL.InstrumentAndConsumables bconsum = new LabMS.BLL.InstrumentAndConsumables();
            try
            {
                for (int i = 0; i < GridViewValuables.Rows.Count; i++)
                {
                    ch = (CheckBox)GridViewValuables.Rows[i].FindControl("checkdel");
                    if (ch.Checked)
                    {
                        string CID = GridViewValuables.DataKeys[i].Value.ToString();
                        bconsum.Delete(long.Parse(CID));
                    }
                }
                //checkall.Checked = false;
                if (tbKeyWords.Text.Trim().Equals("") && tbCreateDateMin.Text.Trim().Equals("") &&
                     tbCreateDateMax.Text.Trim().Equals("") && tbPurpose.Text.Trim().Equals(""))
                {
                    bind("");
                }
                else
                {
                    bind(SearchString());
                }

                Common.JShelper.JSAlert(Page, "success", "删除成功!");
            }
            catch (Exception ex)
            {
                Common.JShelper.JSAlert(Page, "err", "删除失败!");
                Err.Text = ex.Message;
            }
        }

        protected void GridViewValuables_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (UserType != "mana")
            {
                if (e.Row.RowType != DataControlRowType.EmptyDataRow && e.Row.RowType != DataControlRowType.Pager)
                {
                    e.Row.Cells[0].Visible = false;
                }
            }
        }
    }
}
